package com.watson.juc.testlist;

import java.sql.Connection;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author watson
 * @date 2021/7/29 17:51
 * @Description: TODO
 **/
public class testcollection {
//    public static void main(String[] args) {
//        List<Object> list = new ArrayList<>();
//        List<Object> objects = new Vector<>();
//        for (int i = 0; i < 10; i++) {
//            new Thread(() -> {
//                list.add(UUID.randomUUID().toString().substring(0, 5));
//                System.out.println(list);
//            }, String.valueOf(i)).start();
//        }
//        // ConcurrentModificationException 解决方案
//        //1、List<Object> objects = new Vector<>();  后面出来了ArrayList
//        // 2. List<Object> list1 = Collections.synchronizedList(new ArrayList<>());
//        //3.CopyOnWriteArrayList<Object> objects1 = new CopyOnWriteArrayList<>();
//
//        List<Object> object1 = new Vector<>();
//        List<Object> list1 = Collections.synchronizedList(new ArrayList<>());
//        // CopyOnWrite COW 计算机程序设计的一种优化策略
//        //多个线程调用的时候，list，读取的时候是固定的，写入的时候添加覆盖
//        //在写入的时候避免覆盖，
//        CopyOnWriteArrayList<Object> objects1 = new CopyOnWriteArrayList<>();
//
//        //CopyOnWriteArrayList与vector 比较
//        //源码比较  add方法
//
//    }


//    public static void main(String[] args) {
//        Set<Object> objects = Collections.synchronizedSet(new HashSet<>());
//        CopyOnWriteArraySet set = new CopyOnWriteArraySet();
//    }

    public static void main(String[] args) {

        //Map是这样的用的？
        Map<String, Object> map = new HashMap<>();
        // 默认等价于什么
        // Map<String, Object> map1 = new HashMap<>(16, 0.75);
        //加载因子， 容量

        for (int i = 0; i < 300; i++) {
            new Thread(() -> {
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0, 5));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
        // 解决方案
        Map<Object, Object> objectMap = Collections.synchronizedMap(new HashMap<>());
        ConcurrentHashMap<Object, Object> concurrentHashMap = new ConcurrentHashMap<>();

        Hashtable<Object, Object> hashtable = new Hashtable<>();
        //Hashtable 里面p方法使用 synchronized
    }


}
